<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡查线路" prop="lineId">
        <el-select v-model="queryParams.lineId" placeholder="请选择线路" clearable size="small">
          <el-option v-for="line in lineOptions" :label="line.lineName" :value="line.id" :key="line.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="行政小组" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择行政小组" clearable size="small">
          <el-option v-for="group in groupOptions" :label="group.groupName" :value="group.groupId"
                     :key="group.groupId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="巡查日期" prop="date">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.date"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择巡查日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-col :span="1.5" class="mb8">
      <el-button
        type="warning"
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['patrol:site_record_staff:export']"
      >导出
      </el-button>
    </el-col>

    <el-table v-loading="loading" :data="lineStaffList" @selection-change="handleSelectionChange" style="color: #333333"
              :header-cell-style="{background:'#E3E3E3'}">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="学校名称" align="center" prop="line.school.name" v-if="!isShowOperate"/>
      <el-table-column label="学校类别" align="center" prop="line.school.type" :formatter="schoolTypeFormat"
                       v-if="!isShowOperate"/>
      <el-table-column label="巡查日期" align="center" prop="date"/>
      <el-table-column label="线路" align="center" prop="line.lineName"/>
      <el-table-column label="行政小组" align="center" prop="group.groupName"/>
      <el-table-column label="巡查状态" align="center" prop="patrolStatus" :formatter="patrolStatusFormat"/>
      <el-table-column label="巡查结果" align="center" prop="patrolResult">
        <template #default="scope">
          <span v-if="scope.row.patrolStatus > 0">
            <span style="color:#00B3A8 " v-if="scope.row.patrolResult==0">正常</span>
            <span style="color:#DA1E1E " v-if="scope.row.patrolResult>0">{{ scope.row.patrolResult }}项异常</span>
          </span>
          <span v-else>/</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-if="scope.row.patrolStatus>0"
          >查看详情
          </el-button>
          <span v-else>/</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡查线路与行政小组关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="线路" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路">
            <el-option v-for="line in lineOptions" :label="line.lineName" :value="line.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="行政小组" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择行政小组">
            <el-option v-for="group in groupOptions" :label="group.groupName" :value="group.groupId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="巡查日期" prop="date">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.date"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择巡查日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listLineRecordStaff, exportLineStaff} from "@/api/patrol/line_record_staff";
import {listLineAll} from "@/api/patrol/line";
import {listGroupAll} from "@/api/patrol/group";
import {checkPermi} from "@/utils/permission";

export default {
  name: "Staff",
  props: {
    schoolId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡查线路与行政小组关联表格数据
      lineStaffList: [],
      lineOptions: [],
      groupOptions: [],

      // 弹出层标题
      title: "",
      // 是否从首页代办事项进入
      isOuter: false,
      // 首页代办事项传入的日期
      scheduleDateOuter: null,
      // 首页代办事项传入的线路ID
      lineIdOuter: null,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineId: null,
        groupId: null,
        schoolId: null,
        date: null,
        ids: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  computed: {
    isShowOperate: function () {
      return checkPermi(['patrol:guard:edit']);
    },
  },
  created() {
    this.queryParams.schoolId = this.schoolId;
    this.getSchoolOptions();
    this.getLineOptions();
    this.getgroupOptions();
    this.getList();

    this.isOuter = this.$route.query.isOuter;
    this.scheduleDateOuter = this.$route.query.scheduleDate;
    this.lineIdOuter = this.$route.query.lineId;
  },
  mounted() {
    if (this.isOuter) {
      this.isOuter = false;
      this.queryParams.date = this.scheduleDateOuter;
      this.queryParams.lineId = this.lineIdOuter;
      this.handleQuery();
    }
  },
  methods: {
    schoolTypeFormat(row, column) {
      return this.selectDictLabel(this.schoolTypeOptions, row.line.school.type);
    },
    /** 查询巡查线路与行政小组关联列表 */
    getList() {
      this.loading = true;
      listLineRecordStaff(this.queryParams).then(response => {
        this.lineStaffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSchoolOptions() {
      this.loading = true;
      this.getDicts("shool_type").then(response => {
        this.schoolTypeOptions = response.data;
        this.loading = false;
      });
    },
    getLineOptions() {
      this.loading = true;
      listLineAll({"lineType": 1, "schoolId": this.queryParams.schoolId}).then(response => {
        this.lineOptions = response.data;
        this.loading = false;
      })
    },
    getgroupOptions() {
      this.loading = true;
      listGroupAll({"groupType": 0, "groupSchoolId": this.queryParams.schoolId}).then(response => {
        this.groupOptions = response.data;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lineId: null,
        groupId: null,
        date: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleDetail(row) {
      this.$router.push({
        path: '/patrol/record/staff',
        query: {
          scheduleId: row.id,
          lineName: row.line.lineName,
          schoolName: row.line.school.name,
          date: row.date,
          groupName: row.group != null ? row.group.groupName : row.staff.staffName
        },
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLineStaff(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡查线路与行政小组关联";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLineStaff(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLineStaff(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除所选数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return delLineStaff(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      let msg = '是否确认导出选择的巡查记录数据项?';
      if (this.ids.length < 1) {
        msg = "是否确认导出全部的巡查记录数据项?";
      }
      this.queryParams.ids = this.ids
      this.$confirm(msg, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return exportLineStaff(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },

    patrolStatusFormat(row, column) {
      return row.patrolStatus == 0 ? "未开始巡查" : (row.patrolStatus == 1 ? "正在巡查" : "结束巡查");
    },

  }
};
</script>
